/**
 * 认证相关的API服务
 */
import { request } from '@umijs/max';

// 类型定义
export interface LoginParams {
  username: string;
  password: string;
}

export interface RegisterParams {
  username: string;
  email: string;
  password: string;
  role?: string;
}

export interface ChangePasswordParams {
  old_password: string;
  new_password: string;
}

export interface UserInfo {
  id: number;
  username: string;
  email: string;
  role: string;
  created_at: string;
}

export interface LoginResult {
  access_token: string;
  token_type: string;
  expires_in: number;
  user_info: UserInfo;
}

// API接口
export async function login(params: LoginParams): Promise<LoginResult> {
  return request('/api/auth/login', {
    method: 'POST',
    data: params,
  });
}

export async function register(params: RegisterParams): Promise<UserInfo> {
  return request('/api/auth/register', {
    method: 'POST',
    data: params,
  });
}

export async function getCurrentUser(): Promise<UserInfo> {
  return request('/api/auth/me', {
    method: 'GET',
  });
}

export async function changePassword(params: ChangePasswordParams): Promise<{ message: string }> {
  return request('/api/auth/change-password', {
    method: 'POST',
    data: params,
  });
}

export async function logout(): Promise<{ message: string }> {
  return request('/api/auth/logout', {
    method: 'POST',
  });
}

export async function verifyToken(): Promise<UserInfo> {
  return request('/api/auth/verify', {
    method: 'GET',
  });
}
